package com.app.Activity;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
//--

import com.app.Network.Protocol;
import com.app.outdoor.InvoikeServices.HeThongInvoike;
import com.app.outdoor.adapter.ContactAdapter;
import com.app.outdoor.adapter.ContactList;
import com.app.outdoor.pojo.NguoiDungPOJO;

public class od_ContactActivity extends Activity {
	final private int MNU_THEM_THANH_VIEN=4;
	final private int MNU_THEM_NHOM=5;
	ExpandableListView expContact;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.od_activity_contact);
		
		if(CommonUtilities.contacts==null){
			CommonUtilities.contacts = new ContactList();
			getContact();
		}
		
		expContact = (ExpandableListView) findViewById(R.id.expContact);

		ExpandableListAdapter adapter = new ContactAdapter(this, CommonUtilities.contacts);

		expContact.setAdapter(adapter);
		Log.d("Phu", "so con+" + expContact.getChildCount());
		registerForContextMenu(expContact);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		getMenuInflater().inflate(R.menu.od_activity_contact, menu);
		return true;
	}

	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);

		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;

		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		int group = ExpandableListView.getPackedPositionGroup(info.packedPosition);
		int child = ExpandableListView.getPackedPositionChild(info.packedPosition);

		// Only create a context menu for child items

		if (type == 1) {
			menu.add(0, 1, 0, "Chat");
			menu.add(0, 2, 0, "Xóa khỏi nhóm");
		} else {
			menu.add(0, MNU_THEM_THANH_VIEN, 0, "Thêm thành viên");
			menu.add(0, MNU_THEM_NHOM, 0, "Thêm nhóm");
		}
	}

	public boolean onContextItemSelected(MenuItem menuItem) {
		ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
		int groupPos = 0, childPos = 0;
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
		}else if(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){
			if(menuItem.getItemId()==MNU_THEM_THANH_VIEN){
				
			}else if(menuItem.getItemId()==MNU_THEM_NHOM){
				
			}
		}
			
		return super.onContextItemSelected(menuItem);

	}

	private void getContact() {
		try {
			String tenDangNhap = CommonUtilities.nguoiDung.getTenDangNhap();
			String data = HeThongInvoike.getContact(tenDangNhap);
			CommonUtilities.contacts.init(data);
		} catch (Exception e) {
			Log.d("Phu", "Không thể lấy contact");
		}
	}
	
}
